<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 *
 *
 * @package     h
 * @subpackage  models
 * @category    models
 * @author      hyw
 * @copyright   copyright (c) 2012
 * @filesource
 */
class Pair_model extends Base_model {

    protected $_table_name = 'pair';
    protected $_key_name = 'pair_id';


    function insert_verify($url) {

        $a=(rand(0,1))?chr(rand(65,90)):chr(rand(48,57));
        $b=(rand(0,1))?chr(rand(65,90)):chr(rand(48,57));
        $c=(rand(0,1))?chr(rand(65,90)):chr(rand(48,57));
        $d=(rand(0,1))?chr(rand(65,90)):chr(rand(48,57));
        $e=(rand(0,1))?chr(rand(65,90)):chr(rand(48,57));
        $f=(rand(0,1))?chr(rand(65,90)):chr(rand(48,57));
        $g=(rand(0,1))?chr(rand(65,90)):chr(rand(48,57));

        $data = array(
           'code' => $a.$b.$c.$d.$e.$f.$g,
           'url'=>$url
        );
        
        $this->db->insert('verify', $data); 
    }

    function last_verify() {

        
        $this->db->order_by("verify_id", "desc"); 
        $query = $this->db->get('verify'); 
        return $query;
    }
	
	function update_excerpt_sql($id , $data) {

        
		$this->db->where('id', $id);
		$this->db->update('h_excerpt_sql', $data); 
		
		
        //$this->db->where('id' , $id); 
        //$this->db->update('h_excerpt_sql' , array('excerpt_sql'=>$sql));
    }

    function get_verify_url($code){

        $this->db->where('code' ,$code);
        $res=$this->db->get("verify");
        return $res;
    }



    function get_info($id) {
        if (!empty($id))
            $where[$this->_key_name] = $id;
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'row');
    }

    function check_pair($demand_id, $object_id) {
        $con["where"][] = array('demand_id' => $demand_id, 'object_id' => $object_id, 'dtime' => 0);
        $this->set_args_array($con);
        return $this->fetch_count();
    }

    function query_by_where($where = "", $limit) {
        $this->set_limit($limit);
        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }

    function query_by_where_count($where = null) {
        $con["where"][] = array('dtime' => 0);
        $this->set_args_array($con);
        return $this->fetch_count();
    }

    function update_demand($id) {
        $value['demand_status'] = 1;
        $where[] = array('demand_id' => $id);
        return $this->update($value, $where);
    }

    function update_object($id) {
        $value['object_status'] = 1;
        $where[] = array('object_id' => $id);
        return $this->update($value, $where);
    }

    function update_data($data) {
        $value = array('demand_id' => $data['demand_id'], 'object_id' => $data['object_id'], 'status' => 1);
        $where[] = array($this->_key_name => $data['id']);
        return $this->update($value, $where);
    }

    function add($data) {
        $data['ctime'] = time();
        return $this->insert($data);
    }

    function del($data) {
        $where[] = array($this->_key_name => $data['id']);
        $value = array('dtime' => time());
        return $this->update($value, $where);
    }

}
